/**
 * Created by Student on 6/17/14.
 */
 //var app=angular.module
angular.module('mainCtrl',[])
  
            // inject the Comment service into our controller
    .controller('mainController', function($rootScope,$scope, $http, $modal, $log,swastik) {
        // object to hold all the data for the new comment form
        $scope.swastik={}; 
        //console.log($scope.swastik['billdate']);
        console.log($scope.swastik);
        $scope.itemData=[];
        $scope.swastik="5";
                $scope.datepickerOptions = {
                format: 'yyyy-mm-dd',
                language: 'fr',
                startDate: "2012-10-01",
                endDate: "2012-10-31",
                autoclose: true,
                weekStart: 0
            }
        $scope.date = new Date();
        //console.log($scope.date);       

         console.log($scope.swastik);
        //$scope.swastik['billdate']=new Date();
        $scope.edit=false
        // loading variable to show the spinning loading icon
        $scope.loading = true;

        // get all the comments first and bind it to the $scope.comments object
        // use the function we created in our service
        // GET ALL COMMENTS ====================================================
        swastik.get()
            .success(function(data) {
                $scope.partys = data;
                //console.log($scope.simples);
                $scope.loading = false;
            });

        // function to handle submitting the form
         // SAVE A COMMENT ======================================================

         $scope.addData=function(){
            console.log($scope.swastik['qty']);
            $scope.itemData.push({'qty':$scope.swastik['qty'],
                                'itemname':$scope.swastik['itemname'],
                                'detail':$scope.swastik['detail'],
                                'price':$scope.swastik['price'],
                                'amount':$scope.swastik['amount'],
                                'total':$scope.swastik['total']
                                });
         };



        // SAVE A COMMENT ======================================================
        $scope.store = function() {
            $scope.loading = true;

            swastik.save($scope.partyData)
                .success(function(data) {
                            //$scope.simpleData=data;
                            $scope.partyData = {}
                            console.log($scope.partyData);

                    // if successful, we'll need to refresh the comment list
                    swastik.get()
                        .success(function(getData) {
                            $scope.simples = getData;
                            $scope.loading = false;
                        });

                })
                .error(function(data) {
                    console.log(data);
                });
     };

        // function to handle deleting a comment
        // DELETE A COMMENT ====================================================
        $scope.deleteSimple = function(id) {
            $scope.loading = true;
            // console.log(id);   
            // use the function we created in our service
            swastik.destroy(id)
                .success(function(data) {

                    // if successful, we'll need to refresh the comment list
                    swastik.get() 
                        .success(function(getData) {
                            $scope.simples = getData;
                            $scope.loading = false;
                        });

                });
        };

        $scope.editSimple = function(id) {
            $scope.loading = true;
            $scope.edit=true;
             //console.log(id);   
            // use the function we created in our service
            swastik.edit(id)
                .success(function(data) {
                        //console.log(data[0].name)
                            $scope.swastikData =data[0];
                            $scope.loading = false;
                        });

                };       
    $scope.deleteItem = function(item) {
            $scope.loading = true;
             //$scope.edit=false;
             console.log(item);
           //  var a=$scope.rows.indexOf(item);
            // console.log(a);
             $scope.itemData.splice($scope.itemData.indexOf(item),1);
                };       

  $scope.today = function() {
    //$scope.dt = new Date();
  };
  $scope.today();

  $scope.clear = function () {
    $scope.dt = null;
  };

  // Disable weekend selection
  $scope.disabled = function(date, mode) {
    return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) );
  };

  $scope.toggleMin = function() {
    $scope.minDate = $scope.minDate ? null : new Date();
  };
  $scope.toggleMin();

  $scope.open = function($event) {
    $event.preventDefault();
    $event.stopPropagation();

    $scope.opened = true;
  };

  $scope.dateOptions = {
    formatYear: 'yy',
    startingDay: 1
  };

  $scope.initDate = new Date('2016-15-20');
  $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
  $scope.format = $scope.formats[0];
})

